﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TmU220PrintHelperV2d0.ControlUi;
using TmU220PrintHelperV2d0.Entity;
using TmU220PrintHelperV2d0.Service;
using unvell.ReoGrid.Events;

namespace PrinterControl
{
    public partial class FormPrintLogDisplay : Form
    {
        public FormPrintLogDisplay()
        {
            InitializeComponent();
            PrintLog = new PrintLogV2d0();
            PrintLogFiltered = new PrintLogV2d0();
            LogList = new List<rms_order_print_log>();
            Init();
        }
        private void FormPrintLogDisplay_Load(object sender, EventArgs e)
        {
            //printLogDisplay1.Init();
            string caption= "打印日志浏览-0.4-2019-06-23";
            this.Text = caption;
        }
        /// <summary>
        /// 原始日志文件
        /// </summary>
        private PrintLogV2d0 PrintLog { get; set; }
        /// <summary>
        /// 筛选过的日志文件
        /// </summary>
        private PrintLogV2d0 PrintLogFiltered { get; set; }
        /// <summary>
        /// 时间范围内的所有日志列表
        /// </summary>
        private List<rms_order_print_log> LogList { get; set; }
        /// <summary>
        /// 用于显示的日志列表
        /// </summary>
        private List<rms_order_print_log> LogListDisplay { get; set; }

        /// <summary>
        /// 初始化并填充数据到窗口
        /// </summary>
        public void Init()
        {
            //GridWorksheet = reoGridControlRecordDisplay.CurrentWorksheet;
            string printLogXmlFilename = string.Empty;
            DateTime startDatetime = DateTime.Today;
            DateTime endDatetime = DateTime.Today;
            LogList = DatabaseHelperV3d0.GetPrintLog(startDatetime, endDatetime.AddDays(1));
            LogList.ForEach(x => bindingSource_PrintLog.List.Add(x));
            SetupGroupBoxPrinter(LogList);
            SetupGroupBoxTableNo(LogList);
            SetupGroupBoxPrintStatus(LogList);
        }


        /// <summary>
        /// 设置打印机GroupBox
        /// </summary>
        /// <param name="printLog"></param>
        public void SetupGroupBoxPrinter(List<rms_order_print_log> printLog)
        {
            List<string> printerNameList = printLog.Select(x => x.act_printer).Distinct().ToList();
            int positionY = 20;
            groupBoxPrinter.Controls.Clear();
            foreach (var item in printerNameList)
            {
                CheckBox nameCheckBox = new CheckBox();
                nameCheckBox.Tag = "act_printer";
                nameCheckBox.Text = item;
                nameCheckBox.Location = new Point(5, positionY);
                nameCheckBox.AutoSize = true;
                nameCheckBox.Checked = true;
                nameCheckBox.CheckedChanged += checkBox_CheckedChanged;
                positionY += 20;
                groupBoxPrinter.Controls.Add(nameCheckBox);
            }
        }
        /// <summary>
        /// 设置管理员GroupBox
        /// </summary>
        /// <param name="printLog"></param>
        public void SetupGroupBoxPrintStatus(List<rms_order_print_log> printLog)
        {
            List<string> userNameList = printLog.Select(x => x.status.ToString()).Distinct().ToList();
            int positionY = 20;
            foreach (var item in userNameList)
            {
                CheckBox nameCheckBox = new CheckBox();
                nameCheckBox.Tag = "status";
                nameCheckBox.Text = item;
                nameCheckBox.Location = new Point(5, positionY);
                nameCheckBox.AutoSize = true;
                nameCheckBox.Checked = true;
                nameCheckBox.CheckedChanged += checkBox_CheckedChanged;
                positionY += 20;
                groupPrintStatus.Controls.Add(nameCheckBox);
            }
        }
        /// <summary>
        /// 设置台号groupBox
        /// </summary>
        /// <param name="printLog"></param>
        public void SetupGroupBoxTableNo(List<rms_order_print_log> printLog)
        {
            List<string> userNameList = printLog.Select(x => x.tableno).Distinct().ToList();
            int positionY = 20;
            groupBoxTableNo.Controls.Clear();
            foreach (var item in userNameList)
            {
                CheckBox nameCheckBox = new CheckBox();
                nameCheckBox.Tag = "tableno";
                nameCheckBox.Text = item;
                nameCheckBox.Location = new Point(5, positionY);
                nameCheckBox.AutoSize = true;
                nameCheckBox.Checked = true;
                nameCheckBox.CheckedChanged += checkBox_CheckedChanged;
                positionY += 20;
                groupBoxTableNo.Controls.Add(nameCheckBox);
            }
        }

        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox currCheckBox = null;
            if (sender is CheckBox)
            {
                //深度复制（当前全部日志数据）
                LogListDisplay = HotchPotchV2d0.Clone<List<rms_order_print_log>>(LogList);
                //依次根据未选中checkBox剔除对应数据
                //打印机
                foreach (var item in groupBoxPrinter.Controls)
                {
                    currCheckBox = item as CheckBox;
                    if (!currCheckBox.Checked)
                    {
                        string filterString = currCheckBox.Text;
                        LogListDisplay.RemoveAll(x => x.act_printer == filterString);
                    }
                }
                //打印状态
                foreach (var item in groupPrintStatus.Controls)
                {
                    currCheckBox = item as CheckBox;
                    if (!currCheckBox.Checked)
                    {
                        int printStatusTmp = Convert.ToInt32(currCheckBox.Text);
                        LogListDisplay.RemoveAll(x => x.status == printStatusTmp);
                    }
                }
                //台号
                foreach (var item in groupBoxTableNo.Controls)
                {
                    currCheckBox = item as CheckBox;
                    if (!currCheckBox.Checked)
                    {
                        string filterString = currCheckBox.Text;
                        LogListDisplay.RemoveAll(x => x.tableno == filterString);
                    }
                }
            }
            bindingSource_PrintLog.List.Clear();
            LogListDisplay.ForEach(x => bindingSource_PrintLog.List.Add(x));
            //LogList.ForEach(x => bindingSource_PrintLog.List.Add(x));

        }
        /// <summary>
        /// 日历选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            Init();
        }

        private void reoGridControlRecordDisplay_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string currTaskUid = dataGridView1.Rows[e.RowIndex].Cells["task_uid"].Value.ToString();

            if (!string.IsNullOrEmpty(currTaskUid))
            {
                listBoxContent.Items.Clear();
                listBoxProcessDescription.Items.Clear();
                var logSelected = LogList.FirstOrDefault(x => x.task_uid == currTaskUid);
                string[] contentArray = logSelected.print_content.Split(',');
                string[] descriptionArray = logSelected.process_description.Split(',');
                listBoxContent.Items.AddRange(contentArray);
                listBoxProcessDescription.Items.AddRange(descriptionArray);
            }
        }

        private void button_datetime_Click(object sender, EventArgs e)
        {
            DateSelectForm selectForm = new DateSelectForm();
            selectForm.ShowDialog();
            if (selectForm.DialogResult == DialogResult.OK)
            {
                bindingSource_PrintLog.List.Clear();
                DateTime startDatetime = selectForm.monthCalendar1.SelectionStart;
                DateTime endDatetime = selectForm.monthCalendar1.SelectionEnd;
                LogList = DatabaseHelperV3d0.GetPrintLog(startDatetime, endDatetime.AddDays(1));
                LogList.ForEach(x => bindingSource_PrintLog.List.Add(x));
                SetupGroupBoxPrinter(LogList);
                SetupGroupBoxTableNo(LogList);
                SetupGroupBoxPrintStatus(LogList);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
